package com.fas.hehe.system.controller;

import java.util.HashMap;
import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.fas.core.BaseController;
import com.fas.core.constant.R;
import com.fas.core.plugin.jfinal.route.ControllerBind;
import com.fas.core.plugin.tree.jstree.Node;
import com.fas.hehe.system.model.Role;
import com.fas.hehe.system.service.OrganizationService;
import com.fas.hehe.system.service.ResourceService;
import com.fas.hehe.system.service.RoleService;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * 
 * @ClassName: RoleController
 * @Description: 角色管理
 * @Copyright: Copyright (c) 2016 FAS
 * @anthor yixiaoping<thinker1013@163.com>
 * @since 2016年1月26日 上午9:50:13
 * @version V1.0
 */
@RequiresAuthentication
@ControllerBind(controllerKey = "/system/role", viewPath = "/system/role")
public class RoleController extends BaseController {

	public RoleService roleService;
	public ResourceService resourceService;
	public OrganizationService organizationService;

	// 【异步】分页查询
	@RequiresPermissions(value = { R.Authority.Role.QUERY }, logical = Logical.AND)
	public void list() {
		Page<Record> recordList = roleService.queryPage(this.buildPageRequest());
		this.returnPage(recordList);
	}

	// 【异步】一次性加载字典分类树
	@RequiresPermissions(value = { R.Authority.Res.QUERY, }, logical = Logical.AND)
	public void loadOrganizationTreeData() {
		List<Node> treeNodes = organizationService.loadTreeOnceTime(true);// 只加载公司类型机构（总公司|分公司）
		HashMap<String, Object> returnData = new HashMap<String, Object>();
		returnData.put("treeNodes", treeNodes);
		this.returnJsonResult(returnData);
	}

	// 【异步】 根据机构，查询角色树形列表
	@RequiresPermissions(value = { R.Authority.Role.QUERY, }, logical = Logical.AND)
	public void loadRolesByOrgId() {
		List<Node> treeNodes = roleService.loadOrgRoleTree(this.getParaToLong("orgId"));
		HashMap<String, Object> returnData = new HashMap<String, Object>();
		returnData.put("treeNodes", treeNodes);
		this.returnJsonResult(returnData);
	}

	// 【异步】保存角色(新增|修改)
	@RequiresPermissions(value = { R.Authority.Role.CREATE, R.Authority.Role.UPDATE }, logical = Logical.OR)
	public void saveRole() throws Exception {
		Role role = (Role) this.getModelEntity();
		role = roleService.saveRole(role);
		Node node = roleService.buildJstreeNode(role);
		this.returnJsonResult(node);
	}

	// 【异步】删除角色
	@RequiresPermissions(value = { R.Authority.Role.DELETE }, logical = Logical.AND)
	public void deleteRole() {
		boolean isSuccess = SUCCESS;
		String message = "";
		String treeNodeId = this.getPara("treeNodeId"); // treeNodeId： "R_"
														// +roleId,需解析
		Long roleId = roleService.pareseTreeNodeId(treeNodeId);// 解析roleId
		if (roleService.deleteRoleCheck(roleId)) {
			isSuccess = roleService.deleteRole(roleId);
		} else {
			isSuccess = false;
			message = "删除失败！ 该角色存在关联用户";
		}
		this.returnJsonResult(isSuccess, message, null);
	}

	// 【异步】根据角色ID，查询角色对象
	@RequiresPermissions(value = { R.Authority.Role.QUERY }, logical = Logical.AND)
	public void findRoleById() {
		String treeNodeId = this.getPara("treeNodeId");// treeNodeId： "R_"
														// +roleId,需解析
		Long roleId = roleService.pareseTreeNodeId(treeNodeId);// 解析roleId
		Role role = (Role) this.findById(roleId);
		this.returnJsonResult(role);
	}

	// 【异步】根据角色ID加载角色资源列表
	@RequiresPermissions(value = { R.Authority.Res.QUERY, R.Authority.Role.QUERY }, logical = Logical.AND)
	public void loadResourceTreeDataByRole() {
		String treeNodeId = this.getPara("treeNodeId");// treeNodeId： "R_"
														// +roleId,需解析
		Long roleId = roleService.pareseTreeNodeId(treeNodeId);// 解析roleId

		List<Node> treeNodes = roleService.loadResourceTreeByRole(roleId);
		HashMap<String, Object> returnData = new HashMap<String, Object>();
		returnData.put("treeNodes", treeNodes);

		this.returnJsonResult(returnData);
	}

	// 【异步】批量保存角色资源权限
	@RequiresPermissions(value = { R.Authority.Role.CREATE, R.Authority.Role.UPDATE }, logical = Logical.OR)
	public void batchSaveRoleOfResource() {
		String treeNodeId = this.getPara("treeNodeId");
		Long roleId = roleService.pareseTreeNodeId(treeNodeId);

		roleService.batchSaveRoleOfResource(roleId, this.getPara("resourceIds"));// resourceIds是resourceid字符串，多个以逗号分隔
		this.returnJsonResult(SUCCESS);
	}

}
